﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shared.Model;
using Client;
using ManageSystem.Dialog;

namespace ManageSystem.Manager
{
    public partial class FormWorker : UserControl
    {
        private List<WORKER> _listWorker = null;
        private WORKER _choiceWorker = null;

        public FormWorker()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 写List view
        /// </summary>
        /// <param name="worker"></param>
        private void SetListView(WORKER worker)
        {
            if (worker == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = worker.ID.ToString();
            item.SubItems.Add(worker.NAME);
            item.SubItems.Add(worker.SEX);

            if (worker.ENTER_DATE != null)
                item.SubItems.Add(worker.ENTER_DATE.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(worker.CARDID);

            if (worker.FLAG != null)
                item.SubItems.Add(worker.FLAG.ToString());
            else
                item.SubItems.Add("");

            this.listViewWorker.Items.Add(item);
        }
        /// <summary>
        /// 清空list view
        /// </summary>
        /// <param name="view"></param>
        private void ClearView(ListView view)
        {
            view.Items.Clear();
        }
        /// <summary>
        /// 加载员工信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormWorker_Load(object sender, EventArgs e)
        {
            ClearView(this.listViewWorker);
            //加载所有人员信息
            WORKER workerW = new WORKER();
            var selectWorker = MethodWcf.SelectWorkerClient(workerW);

            if (selectWorker.result)
            {
                _listWorker = (from w in selectWorker.value
                               orderby w.ENTER_DATE descending
                               select w).ToList();

                for(int i = 0; i < _listWorker.Count; i++)
                {
                    SetListView(_listWorker[i]);
                }
            }
        }

        //选择员工
        private void listViewWorker_MouseClick(object sender, MouseEventArgs e)
        {
            if (_listWorker.Count == 1)//只有一个人
            {
                _choiceWorker = _listWorker[0];
                return;
            }

            _choiceWorker = (from w in _listWorker
                          where w.ID == long.Parse(this.listViewWorker.SelectedItems[0].Text)
                          select w).FirstOrDefault();

            if (_choiceWorker == null)
            {
                MessageBox.Show("没有找到相应的人员信息");
                return;
            }
        }
        //打开修改员工界面
        private void listViewWorker_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (_choiceWorker != null) buttonChg_Click(null, null);
        }
        //增加
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            UpdateOrInsertWorker ins = new UpdateOrInsertWorker(_listWorker);
            ins.ShowDialog();
            this.FormWorker_Load(null, null);
        }
        //修改
        private void buttonChg_Click(object sender, EventArgs e)
        {
            UpdateOrInsertWorker up = new UpdateOrInsertWorker(_listWorker, _choiceWorker, true);
            up.ShowDialog();
            //刷新员工信息
            this.FormWorker_Load(null, null);
            //修改完后将_choiceWorker设置为空
            _choiceWorker = null;
        }
        //搜索功能
        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            ClearView(this.listViewWorker);//清空链表
            string searchText = this.textBoxSearch.Text.Trim();
            if (string.IsNullOrEmpty(searchText))//如果没有搜索条件则将所有的信息加载进来
            {
                for (int i = 0; i < _listWorker.Count; i++)
                {
                    SetListView(_listWorker[i]);
                }
                return;
            }

            var listSearch = (from w in _listWorker
                              where string.Format("{0}|{1}|{2}|{3}|{4}|{5}",
                              w.ID,
                              w.NAME,
                              w.SEX,
                              w.ENTER_DATE,
                              w.CARDID,
                              w.FLAG)
                              .Contains(searchText)
                              select w).ToList();
            if (listSearch == null || listSearch.Count == 0) return;

            //给List View赋值
            for (int i = 0; i < listSearch.Count; i++)
            {
                SetListView(listSearch[i]);
            }
        }
    }
}
